package com.bawei.module_publish.openlive.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_publish.R
import com.bawei.module_publish.openlive.model.entity.GoodRecord
import com.bumptech.glide.Glide

class LiveGoodsAdapter(
    private var list: MutableList<GoodRecord>,
    private val onExplainClick: (GoodRecord) -> Unit = {},
    private val onMoreClick: (GoodRecord) -> Unit = {},
    private val onItemClick: (GoodRecord) -> Unit = {}
) : RecyclerView.Adapter<LiveGoodsAdapter.LiveGoodsViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveGoodsViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_topcar_goods, parent, false)
        return LiveGoodsViewHolder(view)
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: LiveGoodsViewHolder, position: Int) {
        val good = list[position]
        holder.bind(good)
        holder.btnExplain.setOnLongClickListener{ onExplainClick(good)
        true
        }
        holder.btnMore.setOnClickListener { onMoreClick(good) }
        holder.btnExplain.setOnClickListener { onItemClick(good) }
    }

    fun setData(newList: List<GoodRecord>) {
        list.clear()
        list.addAll(newList)
        notifyDataSetChanged()
    }

    class LiveGoodsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val ivGoodsIcon: ImageView = view.findViewById(R.id.ivGoodsIcon)
        private val tvGoodsName: TextView = view.findViewById(R.id.tvGoodsName)
        private val tvGoodsPrice: TextView = view.findViewById(R.id.tvGoodsPrice)
        private val tvGoodsStock: TextView = view.findViewById(R.id.tvGoodsStock)
        val btnExplain: Button = view.findViewById(R.id.btnExplain)
        val btnMore: ImageButton = view.findViewById(R.id.btnMore)
        fun bind(good: GoodRecord) {
            Glide.with(ivGoodsIcon.context).load(good.mainImgUrl).into(ivGoodsIcon)
            tvGoodsName.text = good.title
            tvGoodsPrice.text = "¥${good.marketPriceFee}起"
            tvGoodsStock.text = "库存${4278} 销量${100}"
        }
    }
} 